package com.iwhalecloud.bss.kite.cucc.service.attr.handler;

import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.constant.SymbolConsts;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @version V1.0
 * @author:lxb
 * @date:2021/4/2
 */
public class HighLevelPackAttrHandler extends AbstractAttrHandler<KiteInst, AttrData> {

    @Override
    public String getValueDesc(KiteAttr attr, KiteInst inst, String value, AttrData data) {
        if (KiteStringUtils.isEmpty(value)) {
            return value;
        }
        List<KiteAttrValue> attrValues = attr.getAttrValues();
        if (KiteListUtils.isEmpty(attrValues)) {
            return value;
        }
        String[] values = KiteStringUtils.split(value, SymbolConsts.COMMA);
        List<String> valueDesc = Arrays.stream(values).map(s -> {
            KiteAttrValue kiteAttrValue = attrValues.stream().filter(attrValue->
                KiteStringUtils.isEqual(attrValue.getAttrValue(), s)).findFirst().orElse(null);
            return Objects.nonNull(kiteAttrValue) ? kiteAttrValue.getAttrValueName() : "";
        }).collect(Collectors.toList());
        return KiteStringUtils.join(valueDesc, SymbolConsts.COMMA);
    }

    @Override
    public void onShow(KiteAttr attr, KiteInst inst, AttrData data) {
        if(KiteStringUtils.equalsAny(inst.getActionType().name(), KeyConsts.ACTION_TYPE_M,KeyConsts.ACTION_TYPE_K)){
            attr.setIsEdit(KeyConsts.IFTRUE_F);
        }
    }

}
